/*
 * Created on Mar 24, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.corpus;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.ModelRDB;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.sdb.SDBFactory;
import com.hp.hpl.jena.sdb.Store;
import com.hp.hpl.jena.sdb.store.StoreFactory;
import com.hp.hpl.jena.shared.ReificationStyle;

/**
 * @author dfhuynh
 */
public class OracleConnector implements Connector {
	/* (non-Javadoc)
	 * @see edu.mit.simile.corpus.IJdbcConnector#connect(java.util.Properties, java.lang.String)
	 */
	public Corpus connect(Properties properties, String corpusURI) {
		System.out.println("CORPUS URI: "+corpusURI+" --- Model Name: "+properties.getProperty("modelName"));
		return new OracleCorpus(
			corpusURI, 
			properties.getProperty("database"),
			properties.getProperty("modelName"),
			properties.getProperty("SDBAssembler")
		);
	}

}

class OracleCorpus extends Corpus {
	static private Logger s_logger = Logger.getLogger(OracleCorpus.class);

	final protected String	m_database;
	final protected String	m_modelName;
	final protected String	m_SDBAssembler;

	protected ModelMaker	m_modelMaker;
	protected Connection	m_connection;

	public OracleCorpus(String corpusURI, String database, String modelName,String SDBAssembler) {
		super(corpusURI);
		m_SDBAssembler=SDBAssembler;
		m_database = database;
		m_modelName = modelName;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.Corpus#dispose()
	 */
	public void dispose() {
		if (m_initialized) {
			if (m_modelMaker != null) {
				m_modelMaker.close();
				m_modelMaker = null;
			}
			if (m_connection != null) {
				try {
					m_connection.close();
				} catch (SQLException e) {
					s_logger.warn(
						"Error closing HSQLDB connection",
						e
					);
				}
				m_connection = null;
			}

			super.dispose();
		}
	}

	protected void createModel() {
		if (m_model != null) {
			return;
		}
		
		//Store store = StoreFactory.create("/home/nicola/Scaricati/SDB-1.3.4/oracle-sdb-assembler.ttl") ;
		Store store = StoreFactory.create(m_SDBAssembler) ;
        m_model = SDBFactory.connectDefaultModel(store) ;
		
//		try {
//			Class.forName("oracle.jdbc.driver.OracleDriver");
//
//			String url =
//				"jdbc:oracle:thin:@localhost:1521:xe";
//
//			m_modelMaker = ModelFactory.createModelRDBMaker(
//				new DBConnection(url, "rivela", "rivela", "Oracle"),
//				ReificationStyle.Standard
//			);
//
//			m_model = m_modelMaker.createModel(m_modelName);
//
//			((ModelRDB) m_model).setDoFastpath(false);
//		} catch (ClassNotFoundException e) {
//			throw new InternalError(
//				"Failed to create Oracle model");
//		}
	}
}
